package com.bawei.hostapp.plugincore;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.text.TextUtils;
import android.util.Log;

import com.bawei.hostapp.ReflectUtils;

import java.lang.reflect.Field;
import java.util.List;

import androidx.annotation.NonNull;

/**
 * @Author : yaotianxue
 * @Time : On 2023/6/6 08:50
 * @Description : MyCallBack
 */
public class HookHmCallback implements Handler.Callback {
    private static final int LAUNCH_ACTIVITY         = 100;
    private static final int EXECUTE_TRANSACTION = 159;
    private static final String TAG = "ytx" ;

    @Override
    public boolean handleMessage(@NonNull Message msg) {
        switch (msg.what){
            // API 21 ~ 27 启动Activity的消息是LAUNCH_ACTIVITY
            case LAUNCH_ACTIVITY:
                Log.d(TAG,"HookHmCallback handleMessage LAUNCH_ACTIVITY enter !!!");
                // 消息对象是ActivityClientRecord对象，其中包含Intent
                // 获取intent对象
                Object intentObject = ReflectUtils.getFieldValue(msg.obj,"intent");
                if(intentObject instanceof  Intent){
                    Intent intent = (Intent) intentObject;
                    // 将之前替换缓存下来的插件Intent替换回去
                    Parcelable actionIntent = intent.getParcelableExtra("actionIntent");
                    if(actionIntent != null){
                        boolean success = ReflectUtils.setField(msg.obj,"intent",actionIntent);
                        if(success){
                            Log.d(TAG,"HookHmCallback handleMessage LAUNCH_ACTIVITY replaced !!!");
                        }
                    }
                }
                break;
            // API 28 ~ 32，添加了事务管理，启动Activity的消息是EXECUTE_TRANSACTION
            case EXECUTE_TRANSACTION:
                Log.d(TAG,"HookHmCallback handleMessage EXECUTE_TRANSACTION enter !!!");
                // 启动Activity之中EXECUTE_TRANSACTION其中一条消息，需要找到属于启动Activity的那条消息
                // 消息对象是ClientTransaction对象，其中有ClientTransactionItem列表
                // 启动Activity的Item是LaunchActivityItem，其中包含Intent
                // 获取mActivityCallbacks，Item列表对象
                Object mActivityCallbacksObject = ReflectUtils.getFieldValue(msg.obj,"mActivityCallbacks");
                if(mActivityCallbacksObject instanceof List){
                    List mActivityCallbacks = (List) mActivityCallbacksObject;
                    // 循环列表
                    for (Object callbackItem : mActivityCallbacks) {
                        // 找到LaunchActivityItem对象
                        if(TextUtils.equals(callbackItem.getClass().getName(),"android.app.servertransaction.LaunchActivityItem")){
                            // 获取LaunchActivityItem的Intent对象
                            Object mIntentObject = ReflectUtils.getFieldValue(callbackItem,"mIntent");
                            if(mIntentObject instanceof Intent){
                                Intent mIntent = (Intent) mIntentObject;
                                // 将之前替换缓存下来的插件Intent替换回去
                                Parcelable actionIntent = mIntent.getParcelableExtra("actionIntent");
                                if(actionIntent != null){
                                    boolean success = ReflectUtils.setField(callbackItem,"mIntent",actionIntent);
                                    if(success){
                                        Log.d(TAG,"HookHmCallback handleMessage EXECUTE_TRANSACTION replaced !!!");
                                    }
                                }
                            }
                        }
                    }
                }
                break;
        }
        return false;
    }
}
